package types

import "encoding/json"

type EventData any

type Event struct {
	Topic string    `json:"topic"`
	Data  EventData `json:"data"`
}

type Error struct {
	Err  error `json:"err"`
	Code int   `json:"code"`
}

func (e Error) Marshal() []byte {
	b, err := json.Marshal(e)
	if err != nil {
		return []byte(err.Error())
	}
	return b
}

func (e Error) ToEvent() Event {
	return Event{Topic: ":error", Data: e}
}

type TopicInfo struct {
	Name string `json:"name"`
	Desc string `json:"desc"`
	App  string `json:"app"`
}
type EventPublisher interface {
	Publish(Event) error
	SessionId() string
}

type ExtendTopicInfo struct {
	TopicInfo
	SubscriberCount int `json:"subcount"`
}
